<template>
  <div class="app-container">
    <el-alert
      title="库存记录显示所有入库、出库和损耗流水，数据仅供查看不可修改"
      type="info"
      :closable="false"
      show-icon
      style="margin-bottom: 20px">
    </el-alert>

    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="SKU编码" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入SKU编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属店铺" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择店铺" clearable>
          <el-option
            v-for="item in shopOptions"
            :key="item.shopId"
            :label="item.shopName"
            :value="item.shopId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="记录类型" prop="recordType">
        <el-select v-model="queryParams.recordType" placeholder="请选择记录类型" clearable>
          <el-option label="入库" value="INBOUND" />
          <el-option label="出库" value="OUTBOUND" />
          <el-option label="损耗" value="LOSS" />
        </el-select>
      </el-form-item>
      <el-form-item label="记录日期">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['stock:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="recordList">
      <el-table-column label="SKU编码" align="center" prop="sku" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="产品名称" align="center" prop="productName" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="所属店铺" align="center" prop="shopName" width="120" />
      <el-table-column label="数量" align="center" prop="quantity" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.recordType === 'INBOUND'" style="color: #67C23A; font-weight: bold;">
            +{{ scope.row.quantity }}
          </span>
          <span v-else style="color: #F56C6C; font-weight: bold;">
            -{{ scope.row.quantity }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="记录类型" align="center" prop="recordType" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.recordType === 'INBOUND'" type="success">入库</el-tag>
          <el-tag v-else-if="scope.row.recordType === 'OUTBOUND'" type="danger">出库</el-tag>
          <el-tag v-else-if="scope.row.recordType === 'LOSS'" type="warning">损耗</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="入库类型" align="center" prop="inboundType" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.inboundType === 'PURCHASE'" type="success">采购入库</el-tag>
          <el-tag v-else-if="scope.row.inboundType === 'RETURN'" type="warning">退货入库</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="记录日期" align="center" prop="recordDate" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recordDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listStockRecord } from "@/api/business/stock"
import { getShopList } from "@/api/system/user"

export default {
  name: "StockRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存记录表格数据
      recordList: [],
      // 日期范围
      dateRange: [],
      // 店铺选项
      shopOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sku: null,
        productName: null,
        shopId: null,
        recordType: null
      }
    }
  },
  created() {
    this.getList()
    this.getShopOptions()
  },
  methods: {
    /** 查询库存记录列表 */
    getList() {
      this.loading = true
      listStockRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.recordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 获取店铺选项 */
    getShopOptions() {
      getShopList().then(response => {
        this.shopOptions = response.data
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/stock/record/export', {
        ...this.queryParams
      }, `stock_record_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

